package com.h3c.nem.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eos.common.util.CustomBeanUtils;
import com.eos.common.util.Result;
import com.h3c.nem.client.AbilityClient;
import com.h3c.nem.entity.TbCareNoticeSetting;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.entity.TbOfferWorkerHis;
import com.h3c.nem.mapper.TbOfferWorkerHisMapper;
import com.h3c.nem.mapper.TbOfferWorkerMapper;
import com.h3c.nem.service.BeiSenService;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ITbWorkerService;
import com.h3c.nem.service.ThreadPoolService;
import com.h3c.nem.service.impl.TbCareEncourageServiceImpl;
import com.h3c.nem.service.impl.TbCareInfoServiceImpl;
import com.h3c.nem.service.impl.TbCareNoticeSettingServiceImpl;
import com.h3c.nem.service.impl.TbOfferFamilyServiceImpl;
import com.h3c.nem.task.CancellationOfferWorker;
import com.h3c.nem.utils.EosUtils;
import com.h3c.nem.utils.WeChatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author z22968
 * @date 2022/6/8 11:55
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/test", produces = "application/json;charset=utf-8")
public class TestController {
    @Resource
    AbilityClient abilityClient;
    @Resource
    TbCareInfoServiceImpl tbCareInfoService;
    @Resource
    TbCareEncourageServiceImpl tbCareEncourageService;
    @Resource
    WeChatUtils weChatUtils;
    @Resource
    CommService commService;
    @Resource
    EosUtils eosUtils;
    @Resource
    ITbWorkerService tbWorkerService;
    @Resource
    TbOfferWorkerMapper tbOfferWorkerMapper;
    @Resource
    CancellationOfferWorker cancellationOfferWorker;
    @Resource
    @Lazy
    BeiSenService beiSenService;
    @Resource
    TbOfferWorkerHisMapper hisMapper;
    @Resource
    ThreadPoolService threadPoolService;
    @Resource
    TbOfferFamilyServiceImpl tbOfferFamilyService;



    @Value("${care.wx.img.url}")
    private String wxImgUrl;
    @Value("${care.index.url}")
    private String careIndexUrl;
    @Value("${wechat.url.appid}")
    private String wechatUrlAppId;
    @Resource
    TbCareNoticeSettingServiceImpl tbCareNoticeSettingService;

    @GetMapping("/new")
    public Result getNewWorker() throws Exception {
//        Map<String, Object> tokenByWechatCode = commService.getTokenByWechatCode("6yFFrcNCR57p9krmsmzB+iuVzstXBNz6N0Wsmk/I6y0=");
//        eosUtils.sendEosMail("zhou.pengb@h3c.com", null, "测试邮件内容2", "测试邮件2");
//        PageQueryDTO<TbCareEncourageVO> queryDTO = new PageQueryDTO<>();
//        TbCareEncourageVO tbCareEncourageVO = new TbCareEncourageVO();
//        tbCareEncourageVO.setStatus(true);
//        queryDTO.setPageNum(1);
//        queryDTO.setPageSize(10);
//        PageInfo<NewWorkerDTO> nonEncourageList = tbCareEncourageService.getEncourageList(queryDTO);
//        weChatUtils.getToken();
//        tbCareInfoService.groupNoticeList();
//        tbCareInfoService.getAllNoticeList();
//        List<NewWorkerDTO> newWorker = sdAllWorkerService.getNewWorker(new Date("2022/01/01"));
//        for (NewWorkerDTO newWorkerDTO : newWorker) {
//            List<TbCareInfo> test = getNoticeLeader.getNoticeLeader(newWorkerDTO);
//            test = getNoticeLeader.getShortCycle(newWorkerDTO, 7);
//            test = getNoticeLeader.getLongCycle(newWorkerDTO, 100,shortCycle);
//            return new Result(false, 44000, "成功", test);
//        }
        return new Result(false, 44000, "成功", null);
    }

    @GetMapping("/message")
    public Result sentMessage() throws UnsupportedEncodingException {
        TbCareNoticeSetting mailTitle = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.MAIL_CONTENT, "鼓励侧标题");
        TbCareNoticeSetting mailContent = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.MAIL_CONTENT, "鼓励侧正文");
        TbCareNoticeSetting weChatTitle = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.WECHAT_CONTENT, "鼓励侧标题");
        TbCareNoticeSetting weChatContent = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.WECHAT_CONTENT, "鼓励侧正文");
        // 发送邮件消息
        eosUtils.sendEosMail("zhou.pengb@h3c.com", null,
                mailTitle.getTextValue(),
                mailContent.getTextValue() + "( 周鹏|z22968 )");
        // 发送企微消息
        weChatUtils.sendWeChatImageMsg("z22968",
                weChatTitle.getTextValue(),
                weChatContent.getTextValue() + "( 周鹏|z22968 )",
                weChatUtils.getWeChatOpenUrl(careIndexUrl));
        return new Result(false, 44000, "成功", null);
    }
    @GetMapping("/message/1")
    public Result sentMessage1() throws UnsupportedEncodingException {
        TbCareNoticeSetting mailTitle = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.MAIL_CONTENT, "绩效优秀标题");
        TbCareNoticeSetting mailContent = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.MAIL_CONTENT, "绩效优秀正文");
        TbCareNoticeSetting weChatTitle = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.WECHAT_CONTENT, "绩效优秀标题");
        TbCareNoticeSetting weChatContent = tbCareNoticeSettingService.selectOne(TbCareNoticeSetting.SettingType.WECHAT_CONTENT, "绩效优秀正文");
        // 发送邮件消息
        eosUtils.sendEosMail("zhou.pengb@h3c.com", null,
                mailTitle.getTextValue(),
                mailContent.getTextValue() + "( 周鹏|z22968 )");
        // 发送企微消息
        weChatUtils.sendWeChatImageMsg("z22968",
                weChatTitle.getTextValue(),
                weChatContent.getTextValue() + "( 周鹏|z22968 )",
                weChatUtils.getWeChatOpenUrl(careIndexUrl));
        return new Result(false, 44000, "成功", null);
    }

    @RequestMapping("/callSapTest")
    @ResponseBody
    public Result callSapTest(HttpServletRequest request) {
        List list = commService.getUser();
        return new Result(true, 20000, " callSapTest 成功", list);
    }

    @RequestMapping("/getCache")
    @ResponseBody
    public Result callSapTest(String key) {
        String v = commService.getCache(key);
        return new Result(true, 20000, v);
    }

    @GetMapping("/insertIntoWorker")
    public void insertIntoWorker(@RequestParam("workerId") String workerId) {
        TbOfferWorker tbOfferWorker = tbOfferWorkerMapper.selectById(workerId);
        if (tbOfferWorker != null) {
            tbWorkerService.insertOfferWorkerIntoWorker(tbOfferWorker);
        }
    }

    @GetMapping("/cancelWorkerTest")
    public void cancelWorkerTest() {
        cancellationOfferWorker.cancelOfferWorkerTest();
    }


    @GetMapping("/syncWorkerToBeiSen")
    public void syncWorkerToBeiSen(@RequestParam("workerId") List<String> workerId) {
        List<TbOfferWorker> tbOfferWorker = tbOfferWorkerMapper.selectBatchIds(workerId);
        if (!CollectionUtils.isEmpty(tbOfferWorker)) {
            tbOfferWorker.forEach(o -> {
                beiSenService.syncWorkerStatusToBeiSen(o);
            });
        }
    }

    @GetMapping("/syncHisToBeiSen")
    public void syncHisToBeiSen() {
        List<TbOfferWorkerHis> tbOfferWorkerHis = hisMapper.selectList(new QueryWrapper<>());
        Thread thread = new Thread(() -> {
            tbOfferWorkerHis.forEach(his -> {
                TbOfferWorker offerWorker = new TbOfferWorker();
                CustomBeanUtils.copyProperties(his, offerWorker);
                if ("北森".equals(offerWorker.getDataSource())) {
                    beiSenService.syncWorkerStatusToBeiSen(offerWorker);
                }
            });
        });
        threadPoolService.submitTask(thread);
    }

    @GetMapping("/getdept")
    public JSONObject getdept(Integer deptId) {
        return abilityClient.getPtDept(deptId);
    }

    @PostMapping("/fixFamily")
    public void fixFamily(@RequestBody JSONObject object) {
        String workerId = object.getString("workerId");
        String[] split = workerId.split(",");

    }

    @PostMapping("/test")
    public void test(@RequestBody JSONObject object) {
        List<String> workerIdList = (List<String>) object.get("workerIdList");
        tbOfferFamilyService.fixFamily(workerIdList);
    }

    @PostMapping("/updateSapNationPlaceSingle")
    public int updateSapNation(@RequestBody JSONObject obj) {
        List<String> workerIds = (List<String>) obj.get("workerId");
        return beiSenService.updateSapNationPlace(workerIds);
    }

    @PostMapping("/updateSapNationPlace")
    public int updateSapNationPlace() {
        return beiSenService.updateSapNationPlaceForWorker();
    }
}
